use eframe::{egui, CreationContext, Frame};

struct MyApp {
    state: AppState,
}

impl MyApp {
    fn default(cc: &CreationContext<'_>) -> Self {
        Self {
            state: AppState::Login(Login {}), // 初始状态为登录页面
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) {
        match &mut self.state {
            AppState::Login(ref  login) => self.ui_login(ctx, login),
            AppState::Index(ref   index) => self.ui_index(ctx, index),
        }
    }
}

impl MyApp {
    fn ui_login(&mut self, ctx: &egui::Context, login: &Login) {
     
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.label("login frame");
            // 假设这里有一个输入框和一个登录按钮
            if ui.button("login").clicked() {
                // 登录成功后切换到 Index 状态，默认进入用户管理
                self.state = AppState::Index(Index {
                    state: IndexState::UserManager,
                });
            }
        });
    }

    fn ui_index(&mut self, ctx: &egui::Context, index:&Index) {
        egui::TopBottomPanel::top("top_panel")
            .resizable(true)
            .min_height(32.0)
            .show(ctx, |ui| {
                ui.horizontal(|ui| {
                    if ui.button("userManager").clicked() {
                        self.state = AppState::Index(Index {
                            state: IndexState::UserManager,
                        });
                    }
                    if ui.button("resourceManager").clicked() {
                        self.state = AppState::Index(Index {
                            state: IndexState::ResourceManager,
                        });
                    }
                });
            });

        // 根据当前的 index.state 渲染不同的内容
        match &mut self.state {
            AppState::Index(index) => {
                match index.state {
                    IndexState::UserManager => {
                        egui::CentralPanel::default().show(ctx, |ui| {
                            ui.label("hwllo");
                            // 在这里添加用户管理的内容
                        });
                    }
                    IndexState::ResourceManager => {
                        egui::CentralPanel::default().show(ctx, |ui| {
                            ui.label("abcefdf");
                            // 在这里添加资源管理的内容
                        });
                    }
                }
            }

            AppState::Login(login) => {}
        }
    }
}

// 定义应用状态
#[derive(Debug)]
enum AppState {
    Login(Login),
    Index(Index),
}

// 定义 Login 结构体
#[derive(Debug)]
struct Login {}

// 定义 Index 结构体
#[derive(Debug)]
struct Index {
    state: IndexState,
}

// 定义 IndexState 类型
#[derive(Debug)]
enum IndexState {
    UserManager,
    ResourceManager,
}

fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        ..Default::default()
    };
    eframe::run_native(
        "XXX  平台",
        options,
        Box::new(|cc| Ok(Box::new(MyApp::default(cc)))),
    )
}
